package com.happy_lab_management.demo.student.lab.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.happy_lab_management.demo.admin.labSchedule.service.LabScheduleService;
import com.happy_lab_management.demo.admin.semester.service.SemesterService;
import com.happy_lab_management.demo.common.section.service.SectionService;
import com.happy_lab_management.demo.common.weekday.service.WeekdayService;
import com.happy_lab_management.demo.system.service.SystemService;
import com.happy_lab_management.demo.technician.lab.service.LabService;
import com.happy_lab_management.entity.*;
import com.happy_lab_management.entity.DTO.LabBorrowApplicationDTO;
import com.happy_lab_management.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController("studentLabController")
@RequestMapping("/student/lab")
public class LabController {
    @Autowired
    private LabService labService;
    @Autowired
    private LabScheduleService labScheduleService;
    @Autowired
    private SemesterService semesterService;
    @Autowired
    private SectionService sectionService;
    @Autowired
    private WeekdayService weekdayService;
    @Autowired
    private SystemService systemService;

    @RequestMapping("add")
    public Result<String> add(@RequestBody Lab lab) {
        labService.save(lab);
        return Result.success("添加实验室成功");
    }

    @RequestMapping("/list")
    public Result<List<Lab>> list(@RequestBody LabBorrowApplicationDTO labBorrowApplicationDTO) {

        QueryWrapper<LabSchedule> queryWrapper = new QueryWrapper<>();

        //第几周
        if(labBorrowApplicationDTO.getWeek()!=null){
            queryWrapper.between("start_week", "end_week", labBorrowApplicationDTO.getWeek());
        }
        //应根据学期、周次、节次检查是否可用
        //获取当前学期查询
        Integer currentSemesterId = systemService.getValueByKey("current_semester_id");
        queryWrapper.eq("semester_id", currentSemesterId);//学期

        //节次
        if(labBorrowApplicationDTO.getLabId()!=null)
            queryWrapper.eq("section", labBorrowApplicationDTO.getLabId());

        //星期几
        if(labBorrowApplicationDTO.getWeekday()!=null)
            queryWrapper.eq("id", labBorrowApplicationDTO.getWeekday());

        // 查询满足条件的 LabSchedule 记录
        List<LabSchedule> labSchedule = labScheduleService.list(queryWrapper);
        //查询出所有的Lab记录


        // 获取满足条件的 LabSchedule 记录的 Lab ID 列表
        List<Integer> labId = labSchedule.stream().map(LabSchedule::getLabId).collect(Collectors.toList());

        // 构造一个新的 QueryWrapper，用于根据 Lab ID 过滤 Lab 表
        QueryWrapper<Lab> labQueryWrapper = new QueryWrapper<>();
        if (!labSchedule.isEmpty()) { //返回所有记录
            labQueryWrapper.notIn("id", labId);
        }
        if(labBorrowApplicationDTO.getLabType()!=null){
            labQueryWrapper.eq("lab_type",labBorrowApplicationDTO.getLabType());
        }
        List<Lab> notLabs = labService.list(labQueryWrapper);


        //检查Lab是否被借用占用
        return Result.success(notLabs);
    }

}

